home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / network / lattice / portlib.lzh / PORTLIB / SYSLOG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-27  |  7.1 KB  |  315 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #if defined(LIBC_SCCS) && !defined(lint)
  19. static char sccsid[] = "@(#)syslog.c    5.16 (Berkeley) 6/27/88";
  20. #endif /* LIBC_SCCS and not lint */
  21.  
  22. /*
  23.  * SYSLOG -- print message on log file
  24.  *
  25.  * This routine looks a lot like printf, except that it
  26.  * outputs to the log file instead of the standard output.
  27.  * Also:
  28.  *    adds a timestamp,
  29.  *    prints the module name in front of the message,
  30.  *    has some other formatting types (or will sometime),
  31.  *    adds a newline on the end of the message.
  32.  *
  33.  * The output of this routine is intended to be read by /etc/syslogd.
  34.  *
  35.  * Author: Eric Allman
  36.  * Modified to use UNIX domain IPC by Ralph Campbell
  37.  * Modified to use fifo's when running under MiNT on the Atari ST/TT/Falcon
  38.  *    by Stephen Usher
  39.  */
  40.  
  41. #include <sys/types.h>
  42.  
  43. #ifndef atarist
  44. #include <sys/socket.h>
  45. #ifdef SYSLOG_INET
  46. #  include <netinet/in.h>
  47. #else
  48. #  ifdef SYSLOG_UNIXAF
  49. #    include <sys/un.h>
  50. #  else
  51. #    error "Must defined one of SYSLOG_INET or SYSLOG_UNIXAF"
  52. #  endif
  53. #endif
  54. #else
  55. /* Not much! :-) */
  56. #endif
  57.  
  58. #include <sys/file.h>
  59. #include <sys/signal.h>
  60. #include <syslog.h>
  61. #ifndef atarist
  62. #include <netdb.h>
  63. #endif
  64. #include <strings.h>
  65. #ifdef SYSV
  66. #include <fcntl.h>
  67. #endif
  68.  
  69. #ifndef atarist
  70. #define    MAXLINE    1024            /* max message size */
  71. #else
  72. #define    MAXLINE    960            /* max message size */
  73. #endif
  74. #ifndef NULL
  75. #define NULL    0            /* manifest */
  76. #endif
  77.  
  78. #define IMPORTANT     LOG_ERR
  79.  
  80. #ifndef atarist
  81. static char    logname[] = "/dev/log";
  82. #else
  83. static char    logname[] = "/pipe/log";
  84. #endif
  85. static char    ctty[] = "/dev/console";
  86.  
  87. static int    LogFile = -1;        /* fd for log */
  88. static int    connected;        /* have done connect */
  89. static int    LogStat    = 0;        /* status bits, set by openlog() */
  90. static char    *LogTag = "syslog";    /* string to tag the entry with */
  91. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  92. static int    LogFacility = LOG_USER;    /* default facility code */
  93.  
  94. #ifdef SYSLOG_INET
  95. static struct sockaddr_in SyslogAddr;    /*  address of loghost */
  96. #else
  97. #  ifdef SYSLOG_UNIXAF
  98. static struct sockaddr_un SyslogAddr;    /* AF_UNIX address of local logger */
  99. #  endif
  100. #endif
  101.  
  102. extern    int errno, sys_nerr;
  103. extern    char *sys_errlist[];
  104.  
  105. syslog(pri, fmt, p0, p1, p2, p3, p4)
  106.     int pri;
  107.     char *fmt;
  108. {
  109.     char buf[MAXLINE + 1], outline[MAXLINE + 1];
  110.     register char *b, *f, *o;
  111.     register int c;
  112.     long now;
  113.     int pid, olderrno = errno;
  114.  
  115.     /* see if we should just throw out this message */
  116.     if ((unsigned) LOG_FAC(pri) >= LOG_NFACILITIES ||
  117.         LOG_MASK(LOG_PRI(pri)) == 0 ||
  118.         (pri &~ (LOG_PRIMASK|LOG_FACMASK)) != 0)
  119.         return;
  120.     if (LogFile < 0 || !connected)
  121.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  122.  
  123.     /* set default facility if none specified */
  124.     if ((pri & LOG_FACMASK) == 0)
  125.         pri |= LogFacility;
  126.  
  127.     /* build the message */
  128.     o = outline;
  129.     (void)sprintf(o, "<%d>", pri);
  130.     o += strlen(o);
  131.     time(&now);
  132.     (void)sprintf(o, "%.15s ", ctime(&now) + 4);
  133.     o += strlen(o);
  134.     if (LogTag) {
  135.         strcpy(o, LogTag);
  136.         o += strlen(o);
  137.     }
  138.     if (LogStat & LOG_PID) {
  139.         (void)sprintf(o, "[%d]", getpid());
  140.         o += strlen(o);
  141.     }
  142.     if (LogTag) {
  143.         strcpy(o, ": ");
  144.         o += 2;
  145.     }
  146.  
  147.     b = buf;
  148.     f = fmt;
  149.     while ((c = *f++) != '\0' && c != '\n' && b < &buf[MAXLINE]) {
  150.         if (c != '%') {
  151.             *b++ = c;
  152.             continue;
  153.         }
  154.         if ((c = *f++) != 'm') {
  155.             *b++ = '%';
  156.             *b++ = c;
  157.             continue;
  158.         }
  159.         if ((unsigned)olderrno > sys_nerr)
  160.             (void)sprintf(b, "error %d", olderrno);
  161.         else
  162.             strcpy(b, sys_errlist[olderrno]);
  163.         b += strlen(b);
  164.     }
  165.     *b++ = '\n';
  166.     *b = '\0';
  167.     (void)sprintf(o, buf, p0, p1, p2, p3, p4);
  168.     c = strlen(outline);
  169.     if (c > MAXLINE)
  170.         c = MAXLINE;
  171.  
  172.     /* output the message to the local logger */
  173. #ifndef atarist
  174.     if (send(LogFile, outline, c, 0) >= 0)
  175. #else
  176.     if (write(LogFile, outline, c) >= 0)
  177. #endif
  178.         return;
  179.     if (!(LogStat & LOG_CONS))
  180.         return;
  181.  
  182.     /* output the message to the console */
  183. #ifdef SYSV
  184.     pid = fork();
  185. #else
  186.     pid = vfork();
  187. #endif
  188.     if (pid == -1)
  189.         return;
  190.     if (pid == 0) {
  191.         int fd;
  192.  
  193. #ifdef SYSV
  194.         signal(SIGALRM, SIG_DFL);
  195. #else
  196.         sigsetmask(sigblock(0L) & ~sigmask(SIGALRM));
  197. #endif
  198.         alarm(5);
  199.         fd = open(ctty, O_WRONLY);
  200.         alarm(0);
  201.         strcat(o, "\r");
  202.         o = index(outline, '>') + 1;
  203.         write(fd, o, c + 1 - (o - outline));
  204.         close(fd);
  205.         _exit(0);
  206.     }
  207.     if (!(LogStat & LOG_NOWAIT))
  208.         while ((c = wait((int *)0)) > 0 && c != pid)
  209.             ;
  210. }
  211.  
  212. /*
  213.  * OPENLOG -- open system log
  214.  */
  215.  
  216. openlog(ident, logstat, logfac)
  217.     char *ident;
  218.     int logstat, logfac;
  219. {
  220. #ifdef SYSLOG_INET
  221.     struct servent *sp;
  222.     struct hostent *hp;
  223. #endif
  224.     if (connected)
  225.         closelog ();
  226.  
  227.     if (ident != NULL)
  228.         LogTag = ident;
  229.     LogStat = logstat;
  230.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  231.         LogFacility = logfac;
  232. #ifdef SYSLOG_INET
  233.     if (LogFile >= 0)
  234.         return(0);
  235.     sp = getservbyname("syslog", "udp");
  236.     hp = gethostbyname(LOG_HOST);
  237.     if (sp != NULL && hp != NULL)
  238.     {
  239.         bzero(&SyslogAddr, sizeof(SyslogAddr));
  240.         SyslogAddr.sin_family = AF_INET;
  241.         bcopy(hp->h_addr, (char *)&SyslogAddr.sin_addr, hp->h_length);
  242.         SyslogAddr.sin_port = sp->s_port;
  243.         if (LogStat & LOG_NDELAY)
  244.             LogFile = socket(AF_INET, SOCK_DGRAM, 0);
  245.     }
  246.     if (LogFile != -1 && !connected &&
  247.         connect(LogFile, (struct sockaddr *) &SyslogAddr,
  248.            sizeof(SyslogAddr)) != -1)
  249.     {
  250.         connected = 1;
  251.         return(0);
  252.     }            
  253.     if (LogStat & LOG_NDELAY)
  254.         return(-1);
  255.     else            
  256.         return(0);
  257. #else
  258. #ifdef SYSLOG_UNIXAF
  259.     if (LogFile == -1) {
  260.         SyslogAddr.sun_family = AF_UNIX;
  261.         strncpy(SyslogAddr.sun_path, logname, sizeof SyslogAddr.sun_path);
  262.         if (LogStat & LOG_NDELAY) {
  263.             LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
  264.             fcntl(LogFile, F_SETFD, 1);
  265.         }
  266.     }
  267.     if (LogFile != -1 && !connected &&
  268.         connect(LogFile, (struct sockaddr *) &SyslogAddr,
  269.             sizeof(SyslogAddr.sun_family) +
  270.             strlen(SyslogAddr.sun_path)) >= 0)
  271.         connected = 1;
  272. #endif
  273. #ifdef atarist
  274.     if (LogFile == -1) {
  275.         LogFile = open(logname, O_RDWR);
  276.     }
  277.     if (LogFile >= 0  && !connected)
  278.     {
  279.         connected = 1;
  280.         return 1;
  281.     } else {
  282.         perror(logname);
  283.         connected = 0;
  284.         return -1;
  285.     }
  286. #endif
  287. #endif
  288. }
  289.  
  290.  
  291. /*
  292.  * CLOSELOG -- close the system log
  293.  */
  294.  
  295. closelog()
  296. {
  297.     (void) close(LogFile);
  298.     LogFile = -1;
  299.     connected = 0;
  300. }
  301.  
  302. /*
  303.  * SETLOGMASK -- set the log mask level
  304.  */
  305. setlogmask(pmask)
  306.     int pmask;
  307. {
  308.     int omask;
  309.  
  310.     omask = LogMask;
  311.     if (pmask != 0)
  312.         LogMask = pmask;
  313.     return (omask);
  314. }
  315.